package com.example.websocketdemo.controller.CIMVUE;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.websocketdemo.service.SHISHI.SHISHIRealTimeTankFarmDataService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

@RestController
@RequestMapping("/VUEMajorLetfTankField")
public class MajorLeftTankFieldController {
    @GetMapping("/Alarm")
    public JSONArray a(HttpServletResponse response) throws IOException {
        response.setHeader("Access-Control-Allow-Origin", "*");
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("报警总次数", "0");
        jsonObject.put("高高 报", "0");
        jsonObject.put("高 报", "0");
        return bianli(jsonObject);
    }

    private static Map<String, Double> temperature;//实时温度
    private static Map<String, Double> realTimeLiquidLevel;//实时液位
    private static Map<String, String> liquidLevel;

    public void storage(String bitNumber, Double AV) {
        if (liquidLevel == null) {
            assignment();
        }
        if (bitNumber.contains("SCADA_A_LT1011")) {
            liquidLevel.put("T101", String.format("%.2f", AV));
            realTimeLiquidLevel.put("T101", AV);
        } else if (bitNumber.contains("SCADA_A_TT1011")) {
            temperature.put("T101", AV);
        } else if (bitNumber.contains("SCADA_A_LT1021")) {
            liquidLevel.put("T102", String.format("%.2f", AV));
            realTimeLiquidLevel.put("T102", AV);
        } else if (bitNumber.contains("SCADA_A_TT1021")) {
            //T102
            temperature.put("T102", AV);
        } else if (bitNumber.contains("SCADA_A_LT1031")) {
            liquidLevel.put("T103", String.format("%.2f", AV));
            realTimeLiquidLevel.put("T103", AV);
        } else if (bitNumber.contains("SCADA_A_TT1031")) {
            //T103
            temperature.put("T103", AV);
        } else if (bitNumber.contains("SCADA_A_LT1041")) {
            liquidLevel.put("T104", String.format("%.2f", AV));
            realTimeLiquidLevel.put("T104", AV);
        } else if (bitNumber.contains("SCADA_A_TT1041")) {
            //T104
            temperature.put("T104", AV);
        } else if (bitNumber.contains("SCADA_A_LT1051")) {
            liquidLevel.put("T105", String.format("%.2f", AV));
            realTimeLiquidLevel.put("T105", AV);
        } else if (bitNumber.contains("SCADA_A_TT1051")) {
            //T105
            temperature.put("T105", AV);
        } else if (bitNumber.contains("SCADA_A_LT1061")) {
            liquidLevel.put("T106", String.format("%.2f", AV));
            realTimeLiquidLevel.put("T106", AV);
        } else if (bitNumber.contains("SCADA_A_TT1061")) {
            //T106
            temperature.put("T106", AV);
        } else if (bitNumber.contains("SCADA_A_LT1071")) {
            liquidLevel.put("T107", String.format("%.2f", AV));
            realTimeLiquidLevel.put("T107", AV);
        } else if (bitNumber.contains("SCADA_A_TT1071")) {
            //T107
            temperature.put("T107", AV);
        } else if (bitNumber.contains("SCADA_A_LT1081")) {
            liquidLevel.put("T108", String.format("%.2f", AV));
            realTimeLiquidLevel.put("T108", AV);
        } else if (bitNumber.contains("SCADA_A_TT1081")) {
            //T108
            temperature.put("T108", AV);
        } else if (bitNumber.contains("SCADA_A_LT1091")) {
            liquidLevel.put("T109", String.format("%.2f", AV));
            realTimeLiquidLevel.put("T109", AV);
        } else if (bitNumber.contains("SCADA_A_TT1091")) {
            //T109
            temperature.put("T109", AV);
        } else if (bitNumber.contains("SCADA_A_LT1101")) {
            liquidLevel.put("T110", String.format("%.2f", AV));
            realTimeLiquidLevel.put("T110", AV);
        } else if (bitNumber.contains("SCADA_A_TT1101")) {
            //T110
            temperature.put("T110", AV);
        } else if (bitNumber.contains("SCADA_A_LT1111")) {
            liquidLevel.put("T111", String.format("%.2f", AV));
            realTimeLiquidLevel.put("T111", AV);
        } else if (bitNumber.contains("SCADA_A_TT1111")) {
            //T111
            temperature.put("T111", AV);
        } else if (bitNumber.contains("SCADA_A_LT1121")) {
            liquidLevel.put("T112", String.format("%.2f", AV));
            realTimeLiquidLevel.put("T112", AV);
        } else if (bitNumber.contains("SCADA_A_TT1121")) {
            //T112
            temperature.put("T112", AV);
        } else if (bitNumber.contains("SCADA_A_LT1131")) {
            liquidLevel.put("T113", String.format("%.2f", AV));
            realTimeLiquidLevel.put("T113", AV);
        } else if (bitNumber.contains("SCADA_A_TT1131")) {
            //T113
            temperature.put("T113", AV);
        } else if (bitNumber.contains("SCADA_A_LT1141")) {
            liquidLevel.put("T114", String.format("%.2f", AV));
            realTimeLiquidLevel.put("T114", AV);
        } else if (bitNumber.contains("SCADA_A_TT1141")) {
            //T114
            temperature.put("T114", AV);
        } else if (bitNumber.contains("SCADA_A_LT1151")) {
            liquidLevel.put("T115", String.format("%.2f", AV));
            realTimeLiquidLevel.put("T115", AV);
        } else if (bitNumber.contains("SCADA_A_TT1151")) {
            //T115
            temperature.put("T115", AV);
        }
    }


    @GetMapping("/data")
    public JSONArray d(HttpServletResponse response) throws IOException {
        response.setHeader("Access-Control-Allow-Origin", "*");
        if (liquidLevel == null) {
            assignment();
        }
        JSONArray jsonArray = new JSONArray();
        JSONObject jsonObject = new JSONObject();
        JSONObject jsonObject1 = new JSONObject();
        JSONObject jsonObject2 = new JSONObject();
        JSONObject jsonObject3 = new JSONObject();
        JSONObject jsonObject4 = new JSONObject();
        List<String> list = new ArrayList<>();
        List<String> list1 = new ArrayList<>();
        List<String> list2 = new ArrayList<>();
        List<String> list3 = new ArrayList<>();
        list.add("4.0");
        list.add("3.2");
        list.add("2.4");
        list.add("1.6");
        list.add("0.0");
//        list1.add("2000");
//        list1.add("1500");
//        list1.add("1200");
//        list1.add("800");
//        list1.add("400");
        for (Map.Entry<String, String> entry : liquidLevel.entrySet()) {
            list1.add(entry.getValue());
        }
        jsonObject.put("xAxis", list);
        jsonObject.put("yAxis", list1);
        jsonObject1.put("data", list2);
        jsonObject1.put("title", "压力");
        jsonObject3.put("pressure", jsonObject1);
        jsonObject2.put("data", list3);
        jsonObject2.put("title", "液位");
        jsonObject4.put("liquid", jsonObject2);
        jsonArray.add(jsonObject);
        jsonArray.add(jsonObject3);
        jsonArray.add(jsonObject4);
        return jsonArray;
    }

    @GetMapping("/rest")
    public JSONArray rest(HttpServletResponse response) throws IOException {
        response.setHeader("Access-Control-Allow-Origin", "*");
        JSONObject jsonObject = new JSONObject();
        int WD = 0;
        double WD_value = 0.0;
        for (Map.Entry<String, Double> entry : temperature.entrySet()) {
            WD_value += entry.getValue();
            WD++;
        }
        int YW = 0;
        double YW_value = 0.0;
        for (Map.Entry<String, Double> entry : realTimeLiquidLevel.entrySet()) {
            YW_value += entry.getValue();
            YW++;
        }
        double WD_mean = WD_value / WD;
        double YM_mean = YW_value / YW;

        jsonObject.put("实时温度", String.format("%.2f", WD_mean) + " ℃");
        jsonObject.put("实时液位", String.format("%.2f", YM_mean) + " MM");
//        jsonObject.put("实施压强", "2 MPR");
        return bianli(jsonObject);
    }

    private void assignment() {
        liquidLevel = new HashMap<>();
        liquidLevel.put("T101", "0");
        liquidLevel.put("T102", "0");
        liquidLevel.put("T103", "0");
        liquidLevel.put("T104", "0");
        liquidLevel.put("T105", "0");
        liquidLevel.put("T106", "0");
        liquidLevel.put("T107", "0");
        liquidLevel.put("T108", "0");
        liquidLevel.put("T109", "0");
        liquidLevel.put("T110", "0");
        liquidLevel.put("T111", "0");
        liquidLevel.put("T112", "0");
        liquidLevel.put("T113", "0");
        liquidLevel.put("T114", "0");
        liquidLevel.put("T115", "0");
        temperature = new HashMap<>();
        temperature.put("T101", 0.0);
        temperature.put("T102", 0.0);
        temperature.put("T103", 0.0);
        temperature.put("T104", 0.0);
        temperature.put("T105", 0.0);
        temperature.put("T106", 0.0);
        temperature.put("T107", 0.0);
        temperature.put("T108", 0.0);
        temperature.put("T109", 0.0);
        temperature.put("T110.0", 0.0);
        temperature.put("T111", 0.0);
        temperature.put("T112", 0.0);
        temperature.put("T113", 0.0);
        temperature.put("T114", 0.0);
        temperature.put("T115", 0.0);
        realTimeLiquidLevel = new HashMap<>();
        realTimeLiquidLevel.put("T101", 0.0);
        realTimeLiquidLevel.put("T102", 0.0);
        realTimeLiquidLevel.put("T103", 0.0);
        realTimeLiquidLevel.put("T104", 0.0);
        realTimeLiquidLevel.put("T105", 0.0);
        realTimeLiquidLevel.put("T106", 0.0);
        realTimeLiquidLevel.put("T107", 0.0);
        realTimeLiquidLevel.put("T108", 0.0);
        realTimeLiquidLevel.put("T109", 0.0);
        realTimeLiquidLevel.put("T110.0", 0.0);
        realTimeLiquidLevel.put("T111", 0.0);
        realTimeLiquidLevel.put("T112", 0.0);
        realTimeLiquidLevel.put("T113", 0.0);
        realTimeLiquidLevel.put("T114", 0.0);
        realTimeLiquidLevel.put("T115", 0.0);
    }

    private JSONArray bianli(JSONObject jsonObject) {
        JSONArray jsonArray = new JSONArray();
//        JSONObject json = new JSONObject();
        Iterator iter = jsonObject.entrySet().iterator();
        while (iter.hasNext()) {
            JSONObject json = new JSONObject();
            Map.Entry entry = (Map.Entry) iter.next();
            json.put("title", entry.getKey().toString());
            json.put("value", entry.getValue().toString());
            jsonArray.add(json);
        }
        return jsonArray;
    }
}
